﻿using System;
using System.Collections;
using Model;
using NHibernate;
using NHibernate.Expression;

namespace DataAccess
{
    public class WODataAccess : BaseDataAccess
    {
        public void SetAuthorizeWorkOrder(int workOrderId)
        {

            try
            {

                WorkOrder workOrder = new WorkOrder();
                ICriteria crit = m_session.CreateCriteria(typeof(WorkOrder));
                crit.Add(Expression.Eq(WorkOrder.CONST_WORK_ORDER_ID, workOrderId));
                if (crit.List().Count != 0)
                {
                    workOrder = crit.List()[0] as WorkOrder;
                    workOrder.Authorized = true;
                    Save(workOrder);
                }
                

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public WorkOrder GetWorkOrder(int workOrderId)
        {       
                WorkOrder workOrder = new WorkOrder();
                ICriteria crit = m_session.CreateCriteria(typeof(WorkOrder));
                crit.Add(Expression.Eq(WorkOrder.CONST_WORK_ORDER_ID, workOrderId));
                if (crit.List().Count != 0) workOrder = crit.List()[0] as WorkOrder;
                return workOrder;
         }
        public WorkOrderTask GetTaskWorkOrder(int taskId)
        {       
                WorkOrderTask workOrder = new WorkOrderTask();
                ICriteria crit = m_session.CreateCriteria(typeof(WorkOrderTask));
                crit.Add(Expression.Eq(WorkOrderTask.CONST_WORKORDER_TASK_ID, taskId));
                if (crit.List().Count != 0) workOrder = crit.List()[0] as WorkOrderTask;
                return workOrder;
         }
        
        public Problem IsAlreadyExistProblem(int prbGroupId, string problemLabel)
        {
            Problem problem = new Problem();
            ICriteria crit = m_session.CreateCriteria(typeof(Problem));
            crit.Add(Expression.Eq(Problem.CONST_GROUP_PROBLEM_ID, prbGroupId));
            crit.Add(Expression.Like(Problem.CONST_PROBLEM_LABEL, problemLabel));
            if (crit.List().Count != 0) problem = crit.List()[0] as Problem;
            return problem;
        }
        public WorkOrderTask GetWorkOrderTasks(int workOrderId, int taskId)
        {
            WorkOrderTask  workOrderTask  = new WorkOrderTask();
            ICriteria crit = m_session.CreateCriteria(typeof(WorkOrderTask));
            crit.Add(Expression.Eq(WorkOrderTask.CONST_WORKORDER_TASK_ID, taskId));
            crit.Add(Expression.Like(WorkOrderTask.CONST_WORKORDER_ID, workOrderId));
            if (crit.List().Count != 0) workOrderTask = crit.List()[0] as WorkOrderTask;
            return  workOrderTask;
        }
        
    }
}
